<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hAdd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list" border>
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success" @click="doAssign(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="hEdit(row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delIcm(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :current-page="pagesParams.page"
                :page-sizes="[2, 3, 5, 10]"
                :page-size="pagesParams.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <el-dialog
        title="编辑弹层"
        :close-on-click-modal="true"
        :close-on-press-escape="true"
        :visible.sync="showDialog"
        @closed="resetForm"
      >
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small">取消</el-button>
            <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <el-dialog title="分配权限" :visible.sync="assignDialog">
        <assignPermissionDialog
          :cur-id="curId"
          @success="assignDialog=false"
        />
      </el-dialog>

    </div>
  </div>
</template>

<script>
import assignPermissionDialog from './assignPermission.vue'
import { addSetting, getSetting, putSetting, delSetting } from '@/api/settings'
export default {
  name: 'VueHrSettings',
  components: {
    assignPermissionDialog
  },

  data() {
    return {
      assignDialog: false, // 控制分配权限的显示和隐藏
      isEdit: false,
      list: [],
      pagesParams: {
        page: 1,
        pagesize: 5
      },
      total: 0,
      roleForm: {
        name: '',
        description: ''
      },
      showDialog: false,
      curId: '',
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.loadSetting()
  },
  methods: {
    // 点击
    doAssign(id) {
      this.curId = id
      this.assignDialog = true
    },
    // 发送获取角色列表请求
    async loadSetting() {
      try {
        const res = await getSetting(this.pagesParams)
        // console.log(res.data.rows)
        this.list = res.data.rows
        this.total = res.data.total
      } catch (error) {
        console.log(error)
      }
    },
    // 显示页面的条数，进行切换时触发 val 每页显示多少条
    handleSizeChange(val) {
      // console.log(val)
      this.pagesParams.page = 1
      this.pagesParams.pagesize = val
      this.loadSetting()
    },
    handleCurrentChange(val) {
      // console.log(val)
      this.pagesParams.page = val
      this.loadSetting()
    },
    // 兜底校验
    hSubmit() {
      this.$refs.roleForm.validate(valid => {
        if (!valid) return
        // 判断是增加还是编辑
        this.isEdit ? this.doEdit() : this.doAdd()
      })
    },
    // 增加
    hAdd() {
      this.isEdit = false
      this.showDialog = true
    },
    // 编辑 实现回显
    hEdit(row) {
      // console.log(row, 'row')
      this.isEdit = true
      this.showDialog = true
      //  运用展开运算符浅拷贝进行回显
      this.roleForm = { ...row }
    },
    // 实现编辑功能
    async doEdit() {
      try {
        // 发送编辑请求
        const res = await putSetting(this.roleForm)
        // console.log(res)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.loadSetting()
        this.showDialog = false
      } catch (error) {
        console.log(error)
      }
    },
    // 新增角色
    async doAdd() {
      try {
        // 发送请求
        const res = await addSetting(this.roleForm)
        // console.log(res)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 添加的时候跳转到最后显示刚刚添加的内容
        this.total++
        const maxPage = Math.ceil(this.total / this.pagesParams.pagesize)
        this.pagesParams.page = maxPage
        this.loadSetting()
        this.showDialog = false
      } catch (error) {
        console.log(error)
      }
    },
    // 重置表单
    resetForm() {
      // 先重置表单验证规则
      this.$refs.roleForm.resetFields()
      // 给表单里面的内容清空
      this.roleForm = {
        name: '',
        description: ''
      }
    },
    // 删除部门
    delIcm(id) {
      this.$confirm('此操作将永久删除该角色吗？ 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // this.$message({
        //   type: 'success',
        //   message: '删除成功!'
        // })
        // 发送删除请求
        const res = await delSetting(id)
        // console.log(res)
        if (res.code === 10000) {
          this.$message.success(res.message)
          // 判断当前页面的数据是否是最后一条
          if (this.list.length === 1) {
            this.pagesParams.page--
            // 边界判断
            if (this.pagesParams.page <= 0) {
              this.pagesParams.page = 1
            }
          }
          this.loadSetting()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }

  }
}
</script>

